export default {
  namespaced: true,
  state: {
    tag: "下方购物的state模块数据",
    carts: [],
  },
  mutations: {
    SET_CART(state, payload) {
      let targetIndex = state.carts.findIndex((item) => item.id === payload.id);

      if (targetIndex == -1) {
        state.carts.push({
          id: payload.id,
          title: payload.title,
          price: payload.price,
          num: 1,
        });
      } else {
        state.carts[targetIndex].num++;
      }
    },
  },
  actions: {
    ADD_PRODUCT(context, payload) {
      //   context.commit("SET_CART", payload);
    },
  },
  getters: {
    total(state) {
      return state.carts.reduce((total, item) => {
        return (total += item.num * item.price);
      }, 0);
      // .toFixed(2);
    },
  },
};
